$('.select2').select2()
// $('#reservation').daterangepicker({
// 	locale:{format:'YYYY-MM-DD'}
// });
// $('#reservation').val('请选择时间');

$(document).ready(function () {
    var checkKind = 1;
    $('.check1').click(function () {
        checkKind = 1;
        getData1(1);
    })
    $('.check2').click(function () {
        checkKind = 2;
        getData2(1);
    })

    function getData1(curr) {
        // var data_rang = $('#reservation').val();//时间范围

        // var start = data_rang.split(' -')[0];
        // var end = data_rang.split('- ')[1];
        var timeOnoff = "False";
        // if(data_rang!=''&&data_rang!='请选择时间'){
        // 	timeOnoff = "True"
        // }
        var gg = layer.load(1, {
            shade: [0.4, '#000'],
        });
        $.ajax({
            type: "get",
            url: "http://www.puer.zone:30000/ProductListInfo",
            async: true,
            data: {
                page: curr,
                row: 10,
                Content: $('.sear_text').val()
            },
            success: function (data) {
                layer.close(gg);
                if (data[0].length == 0) {
                    layer.msg('暂无数据');
                }
                TableView(data[0]);
                pageDivd(curr, data[1]);

            }
        });
    }

    function getData2(curr) {
        var kind = $('.kind_sel').find('.select2-selection__rendered').text();
        var label = $('.label_sel').find('.select2-selection__rendered').text();
        $.ajax({
            type: "get",
            url: "https://www.hgxrshn.com/MgProduct2",
            async: true,
            data: {
                page: curr,
                row: 6,
                kind: kind,
                label: label
            },
            success: function (data) {
                if (data[0].length == 0) {
                    layer.msg('暂无数据');
                }
                TableView(data[0]);
                pageDivd(curr, data[1]);
            }
        });
    }

    getData1(1);

    function pageDivd(curr, page_num) {
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage
                , layer = layui.layer;
            laypage.render({
                elem: 'demo7'
                , count: page_num
                , limit: 10
                , curr: curr
                , jump: function (obj, first) {
                    if (!first) {
                        if (checkKind == 1) {
                            getData1(obj.curr);
                        } else {
                            getData2(obj.curr);
                        }

                    }
                }
            });
        })
    }

    function TableView(data) {
        $('.table_data').html('');
        for (var i = 0; i < data.length; i++) {
            // var radio1,radio2,radio3;
            // if(data[i][6].indexOf('新品')>-1){
            // 	radio1 = '<input type="radio" checked="checked" disabled="disabled">';
            // }else{
            // 	radio1 = '<input type="radio" disabled="disabled">';
            // }
            // if(data[i][6].indexOf('热卖')>-1){
            // 	radio2 = '<input type="radio" checked="checked" disabled="disabled">';
            // }else{
            // 	radio2 = '<input type="radio" disabled="disabled">';
            // }
            // if(data[i][6].indexOf('推荐')>-1){
            // 	radio3 = '<input type="radio" checked="checked" disabled="disabled">';
            // }else{
            // 	radio3 = '<input type="radio" disabled="disabled">';
            // }

            var trText = `<tr>
		      		    		<!--<td><input  index="${data[i][0]}" type="checkbox" /></input></td>-->
		      		    		<td>${data[i][0]}</td>
		      		    		<td>${data[i][1]}</td>
		      		    		<td>
		      		    			<img src="${data[i][2]}" height="50"/>
		      		    		</td>
		      		    		<td>
		      		    			${data[i][3]}
		      		    		</td>
		      		    		<td>
		      		    			￥${data[i][4]}
		      		    		</td>
		      		 
		      		    		<td index="${data[i][0]}">
		      		    			<div>
		      		    				<a class="detail" href="#">查看</a>
		      		    				<a class="go_down" href="#">删除</a>
		      		    			</div>
		      		    			<div>
		      		    				<a class='add_btn' href="#">新增</a>
		      		    				<a class="edit_btn" href="#">修改</a>
		      		    			</div>
		      		    		</td>
		      		    	</tr>`;

            $('.table_data').append(trText);

        }

        lookDetail();
    }

    $("#check_all").click(function () {
        if ($(this).prop('checked') == true) {

            $("tbody").find('input[type=checkbox]').prop('checked', 'checked')
        } else {
            $("tbody").find('input[type=checkbox]').prop('checked', false)
        }
    })

    function lookDetail() {
        $('.detail').click(function () {
            var Index = $(this).parent().parent().attr('index');
            window.location.href = "http://www.puer.zone:9000/product_details.html?Index=" + Index;
        })
        $('.add_btn').click(function () {
            var Index = $(this).parent().parent().attr('index');
            window.location.href = "http://www.puer.zone:9000/new_product2.html?Index=" + Index;
        })
        $('.edit_btn').click(function () {
            var Index = $(this).parent().parent().attr('index');
            window.location.href = "http://www.puer.zone:9000/new_product3.html?Index=" + Index;
        })
        $('.go_down').click(function () {
            var Index = $(this).parent().parent().attr('index');
            layer.confirm('确定要删除此商品吗？', {
                title: ['提示'],
                btn: ['确定', '取消'] //按钮
            }, function () {
                // var CheckIn = $("tbody").find('input[type=checkbox]:checked');
                // // console.log(CheckIn.length)
                // var IndexArry = [];
                // if(CheckIn.length>0){
                //     for (var i=0;i<CheckIn.length;i++) {
                //         IndexArry.push(CheckIn.eq(i).attr('index'));
                //     }
                // }else{
                //     IndexArry.push(Index)
                // }
                // var self = $(this);
                $.ajax({
                    type: "get",
                    url: "http://www.puer.zone:30000/DeleteProduct",
                    async: true,
                    data: {
                        productId: Index
                    },
                    success: function (data) {
                        if (data == 'success') {
                            layer.msg('成功删除', {time: 500}, function () {
                                location.reload();
                            })
                        } else {
                            layer.msg('操作失败')
                        }

                    }

                })
            }, function () {
            });
        })

    }

})